$(document).ready (function() {  

	var originalPDFUrl = $("#generateSalesPDFReport").attr("href");
	var originalXLSUrl = $("#generateSalesXLSReport").attr("href");
  
  $(function() {
    $(".datePicker").datepicker({
     dateFormat: 'dd-mm-yy',
     changeMonth: true,
     changeYear: true,
     yearRange: "-90:+1"});
  });
 
  getQuery = function() {
	  var dateFrom = $('#dateFrom').val();
	  var dateEnd = $('#dateEnd').val();
	  var factAChecked = $('#facturaA')[0].checked;
	  var factBChecked = $('#facturaB')[0].checked;
	  var remitoChecked = $('#remito')[0].checked;
	  var noneFactChecked = $('#noFact')[0].checked;
	  var onlyPayed = $('#onlyPayed')[0].checked;
	  var onlyUnpayed = $('#onlyUnpayed')[0].checked;
	  var query = '';
	  if (dateFrom != '') {
		  query = '&dateFrom=' + dateFrom; 
	  }
	  if (dateEnd != '') {
		  query = query + '&dateEnd=' + dateEnd; 
	  }
	  if (factAChecked) {
		    query = query + "&facturaA=true";
	  }
	  if (factBChecked) {
		    query = query + "&facturaB=true";
	  }
	  if (remitoChecked) {
		    query = query + "&remito=true";
	  }
	  if (noneFactChecked) {
		    query = query + "&noFact=true";
	  }
	  if (onlyPayed) {
		  query = query + "&payedOption=ONLY_PAYED";
	  } else if (onlyUnpayed) {
		  query = query + "&payedOption=ONLY_UNPAYED";
	  }
	  return query;
  };
  
  reloadPage = function(page) {
	 var query = getQuery();
	 $('#tableResult').load(baseweb + "/module/sale/filterSales.do?decorate=none&page=" + page + query);
  }
  
  $('#filter').click(function() {
	  reloadPage(0);
  });

  $("#generateSalesPDFReport").click(function() {
	  var query = getQuery();
	  var url = originalPDFUrl + "?decorate=none" + query;
	  $("#generateSalesPDFReport").attr("href", url);  
  });
  
  $("#generateSalesXLSReport").click(function() {
	  var query = getQuery();
	  var url = originalXLSUrl + "?decorate=none" + query;
	  $("#generateSalesXLSReport").attr("href", url);  
  });

});